package com.zhenyi.jiahua.index.admin;

import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.weixin.sdk.kit.IpKit;
import com.jiahua.common.model.JhStaff;
import com.utils.AESUtils;
import com.utils.AuthUtils;
import com.wxpay.utils.MD5Util;
import com.zhenyi.jiahua.index.JHController;
import com.zhenyi.jiahua.interceptor.AuthInterceptor;
import com.zhenyi.jiahua.interceptor.PermissionInterceptor;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.URLUtil;

@Before({ AuthInterceptor.class, PermissionInterceptor.class })
public class AuthController extends JHController {

	/**
	 * 登录页面
	 */
	@Clear
	public void index() {
		if (StrKit.notBlank(getPara("state"))) {
			String state = URLUtil.decode(getPara("state"));
			setAttr("URL", state);
		}
		// if (StrKit.notBlank(AuthUtils.getTokenUid(this))) {
		// redirect("");
		// }
		String key = RandomUtil.randomString(16);
		setAttr("key", key);
		setSessionAttr("key", key);
		render("/jh/admin/login.html");
	}

	@Clear
	public void login() {
		String account = getPara("account");
		String password = getPara("password");
		try {
			String key = getSessionAttr("key");
			password = AESUtils.decrypt(key, password);
			if (password == null) {
				renderFailure(500, "解密失败,请刷新重试");
				return;
			}

			SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindLoingStaff", account, password);
			JhStaff user = JhStaff.dao.findFirst(sqlPara);
			if (user == null) {
				renderFailure(500, "账号/密码错误");
				return;
			}
			if (!DEBUG) {
				if (user.getFlag() != null) {
					renderFailure(500, "用户不存在");
					return;
				}
				if (YES.equals(user.getIsLeave())) {
					renderFailure(500, "账号已被限定");
					return;
				}
			}

			String uid = String.valueOf(user.getId());
			String oldToken = user.getToken();
			if (StrKit.notBlank(oldToken)) {
				AuthUtils.logout(oldToken);
			}
			String token = AuthUtils.login(uid, this);
			String os = getOS();
			String osm = MD5Util.MD5Encode(os, "utf-8");
			setCookie("osm", osm, -1);
			user.setToken(token);
			user.setLoginTime(new Date());
			user.setLoginCount(user.getLoginCount() + 1);
			// String ip = IpKit.getRealIpV2(getRequest());
			// LogKit.info(user.getName() + " " + user.getPosition() + " " + ip
			// + " " + os + " " + getPlatform());
			user.update();

			String redirectUrl = "/jh/admin/student/list";
			if (JhStaff.TYPE_PD.equals(user.getType())) {
				redirectUrl = "/jh/admin/staff/list";
			} else if (JhStaff.TYPE_CC.equals(user.getType())) {
				redirectUrl = "/jh/admin/student/list?type=0";
			} else if (JhStaff.TYPE_TMK.equals(user.getType())) {
				redirectUrl = "/jh/admin/student/list?type=0";
			} else if (JhStaff.TYPE_TUIGUANG.equals(user.getType())) {
				redirectUrl = "/jh/admin/student/list?type=0";
			} else if (JhStaff.TYPE_TA.equals(user.getType())) {
				redirectUrl = "/jh/admin/student/list?type=1";
			} else if (JhStaff.TYPE_TEACHER.equals(user.getType())) {
				redirectUrl = "/jh/admin/student/list?type=1";
			} else if (JhStaff.TYPE_FIN.equals(user.getType())) {
				redirectUrl = "/jh/admin/student/list?type=1";
			} else if (JhStaff.TYPE_FRONT.equals(user.getType())) {
				redirectUrl = "/jh/admin/student/list?type=0";
			}
			renderSuccess(redirectUrl);
		} catch (Exception e) {
			e.printStackTrace();
			renderFailure("账号/密码错误 错误代码:100500");
			return;
		}
	}

	private final static String CHANGE_PWD_KEY = "4ep18o2t5kkopx2r";

	/**
	 * 修改密码
	 */
	public void changePassword() {
		// String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
		String password = getPara("password");
		password = AESUtils.decrypt(CHANGE_PWD_KEY, password);
		// boolean flag = password.matches(regex);
		// if (flag) {
		// renderMsg("密码由6-16位数字或这字母组成");
		// return;
		// }
		JhStaff staff = getStaff();
		staff.setPassword(password);
		// staff.print();
		staff.update();
		renderSuccess();
	}

	public void logout() {
		AuthUtils.logout(this);
		redirect("/jh/auth");
	}

	public void register() {
	}
}
